# -*- coding: UTF-8 -*-

from cell import Cell


class Map:
    map_cells = dict()
    xs = list()
    ys = list()
    width = 0
    height = 0

    def __init__(self, width, height):
        self.width = width
        self.height = height
        self.xs = map(lambda _x: _x * 10, range(width))
        self.ys = map(lambda _y: _y * 10, range(height))
        for y in self.ys:
            for x in self.xs:
                self.map_cells["{0}_{1}".format(x, y)] = Cell(x, y)

    def find_cells(self, cell):
        "找出 cell 范围内坐标点"
        # 查找范围内点
        min_x = cell.x
        max_x = cell.x + cell.width
        # print min_x, max_x
        min_y = cell.y
        max_y = cell.y + cell.height
        # print min_y, max_y
        # 4个顶点名称
        _nodes = [
            "{0}_{1}".format(min_x, min_y),
            "{0}_{1}".format(min_x, max_y),
            "{0}_{1}".format(max_x, min_y),
            "{0}_{1}".format(max_x, max_y)
        ]
        _xs = filter(lambda x: min_x <= x <= max_x, self.xs)
        _ys = filter(lambda y: min_y <= y <= max_y, self.ys)
        # print _xs
        # print _ys
        # 获取范围内点
        cells = list()
        for _x in _xs:
            for _y in _ys:
                cells.append(self.map_cells.get("{0}_{1}".format(_x, _y)))
        # 场景一：包含一个完整区域，即 cells->4 个点，直接返回最小点即可
        if len(cells) == 4:
            return sorted(cells)[0]
        else:
            pass
        # 场景二：被一个区域完整包含
        # 有一个顶点与标记坐标点重合
        _ins = list(set(_nodes) & set(self.map_cells.keys()))
        # print _ins
        if len(_ins) == 1:
            idx = _nodes.index(_ins[0])
            cell = self.map_cells.get(_ins[0])
            x = cell.x - cell.width
            y = cell.y - cell.height
            if idx == 3:
                pass
            elif idx == 1:
                x = cell.x
            elif idx == 2:
                y = cell.y
            elif idx == 0:
                return cell
            return self.map_cells.get("{0}_{1}".format(x, y))
        # 完全包含在一个格子内，创建 Cell 时避免
        else:
            pass
        # 跨越多个格子，面积最大作为目标格子
        if len(cells) == 1:
            cell = cells[0]
            print cell
            x = cell.x
            y = cell.y
            areas = [
                abs(x - min_x) * abs(y - min_y),
                abs(x - min_x) * abs(y - max_y),
                abs(x - max_x) * abs(y - min_y),
                abs(x - max_x) * abs(y - max_y)
            ]
            idx = areas.index(max(areas))
            if idx == 3:
                return cell
            elif idx == 0:
                x -= cell.width
                y -= cell.height
            elif idx == 1:
                x -= cell.width
            elif idx == 2:
                y -= cell.height
            return self.map_cells.get("{0}_{1}".format(x, y))

    def find_cell(self, cell):
        "查找 cell 对角线交点所在格子"
        # 取对角线交叉点
        cx = cell.x + cell.width / 2
        cy = cell.y + cell.height / 2
        print cx, cy
        x = filter(lambda _x: 0 <= cx - _x < cell.width, self.xs)[0]
        y = filter(lambda _y: 0 <= cy - _y <= cell.height, self.ys)[0]
        print x, y
        return self.map_cells.get("{0}_{1}".format(x, y))


m = Map(10, 10)
# c = m.find_cells(Cell(10, 10))
# print "***", c
# c = m.find_cells(Cell(2, 2, 8, 8))
# print "***", c
c = m.find_cells(Cell(8, 2))
print "***", c
